#coding: utf-8

import constants as CS

class ObjectProperty(object):
    '''
        Classe que encapsula os atributos básicos de toda 
        propriedade de um EngineObject. Toda proprieda deve
        extender esta classe 
    '''
    
    ENUM_TYPE_STR = 0
    ENUM_TYPE_INT = 1
    ENUM_TYPE_DOUBLE = 2
    
    def __init__(self,type,name,label,defaultValue=None):
        self.type = type
        self.name = name
        self.label = label        
        self.value = defaultValue
    
    @apply
    def type():
        def fget(self):
            return self._type
        def fset(self,value):
            self._type = value
        def fdel(self):
            del self._type
            
        return property(**locals())
    
    @apply
    def name():
        def fget(self):
            return self._name
        def fset(self,value):
            self._name = value
        def fdel(self):
            del self._name
            
        return property(**locals())    
    
    @apply
    def label():
        def fget(self):
            return self._label
        def fset(self,value):
            self._label = value
        def fdel(self):
            del self._label
            
        return property(**locals())
    
    @apply
    def value():
        def fget(self):
            return self._value
        def fset(self,value):
            self._valueStr = str(value)
            
            if self.type==ObjectProperty.ENUM_TYPE_INT:
                self._value = int(value)
            elif self.type==ObjectProperty.ENUM_TYPE_DOUBLE:
                self._value = float(value)
            else:
                self._value = value
            
            
        def fdel(self):
            del self._value
            
        return property(**locals())        
    

class PropertyContainer(object):
    '''
        Classe container usada para agrupar as propriedades
        referentes a uma coleção de propriedades. Ela é 
        implementada de forma que é possível acessar seus 
        itens a partir da sintaxe de [] ou iteração a partir
        do comando for in. São instâncias dessa classe que
        são manipuladas pelo renderizador/InspectObject. 
    '''
    def __init__(self,pcc):
        self._pcc = pcc #propertyCollectionClass
        self._namesByOrder = []
        self.properties = "Inicializar"
                     
    def _pget(self):
        return self._properties

    def _pset(self,value):
        if not hasattr(self,"_properties"):
            self._properties = {}
            self.__setP__( self._pcc.__get_all_properties__() )                
            
    def _pdel(self):
        del self._properties

    properties = property(_pget,_pset,_pdel)
            
    def __setP__(self,objPrList):        
        for o in objPrList:
            self._properties.update( {o.name:o} )
            self._namesByOrder.append(o.name)
            
    
    def __getitem__(self,key):
        if type(key)!=str:
            raise TypeError()
        nameId = self._pcc.__get_pid__(key)
        if not self.properties.__contains__(nameId):
            raise KeyError()
        
        return self.properties[ nameId ]
    
    def __len__(self):
        return self.properties.__len__()
    
    def __iter__(self):
        for name in self._namesByOrder:
            yield self.properties[name]
        

class RectSurfacePropertiesCollection(object):
    '''
        Coleção de propriedades referentes ao EngineObject
        RectSurfacePotential
    '''
    POTENTIAL_TOP = "potential_top"
    POTENTIAL_LEFT = "potential_left"
    POTENTIAL_RIGHT = "potential_right"
    POTENTIAL_BOTTOM = "potential_bottom"
    HEIGHT = "height"
    WIDTH = "width"
            
    def __get_pid__(self,name):
        return "rect_surf_%s" %(name)
    
    def __get_all_properties__(self):        
        return [ ObjectProperty(ObjectProperty.ENUM_TYPE_DOUBLE,self.__get_pid__(self.POTENTIAL_TOP), CS.TXT_PRO_POTENTIAL_TOP,"10") 
                ,ObjectProperty(ObjectProperty.ENUM_TYPE_DOUBLE,self.__get_pid__(self.POTENTIAL_RIGHT), CS.TXT_PRO_POTENTIAL_RIGHT,"0")
                ,ObjectProperty(ObjectProperty.ENUM_TYPE_DOUBLE,self.__get_pid__(self.POTENTIAL_BOTTOM), CS.TXT_PRO_POTENTIAL_BOTTOM,"0")
                ,ObjectProperty(ObjectProperty.ENUM_TYPE_DOUBLE,self.__get_pid__(self.POTENTIAL_LEFT), CS.TXT_PRO_POTENTIAL_LEFT,"0")                
                ,ObjectProperty(ObjectProperty.ENUM_TYPE_DOUBLE,self.__get_pid__(self.HEIGHT), CS.TXT_PRO_HEIGHT,"100")
                ,ObjectProperty(ObjectProperty.ENUM_TYPE_DOUBLE,self.__get_pid__(self.WIDTH), CS.TXT_PRO_WIDTH,"100")
                ]
        
#rsp = PropertyContainer(RectSurfacePropertiesCollection())


    